/*
 * Copyright (C) 2009 Umin (Umin90@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include "filer.h"

file_type_ext_struct ext[]={
    {"wav",FS_PCM_FILE},
    {"ape",FS_MAC_FILE},
    {"flac",FS_FLAC_FILE},
    {"tta",FS_TTA_FILE},
    {"wv",FS_WAVPACK_FILE},
    {"aac",FS_AAC_FILE},
    {"m4a",FS_M4A_FILE},
    {"ogg",FS_OGG_FILE},
    {"mp3",FS_MP3_FILE},
    {"mpc",FS_MPC_FILE},
    {"wma",FS_WMA_FILE},
    {"at3",FS_AT3_FILE},
    {"aa3",FS_AA3_FILE},
    {"oma",FS_AA3_FILE},
    {"omg",FS_AA3_FILE},
    {"cue",FS_CUE_FILE},
    {NULL,FS_UNKNOW_FILE}
};

directory_item_struct *items;
int max=0,maxItem;
char *short;


#define listDir(p, s) open_directory(p,s,1,0,ext,&items)

/* samples:

  int i;
  for(i=0;i<fileItemBottom;i++) {
    if( fileItemTop + i < fileItemCount) {
      textColor = fileListTextColor;
      if( fileItemTop + i == fileItemCurrent ) {
        fillImageRect(drawImage, fileListHLBackgroundColor, 
          fileListBoxLeft, fileListBoxTop+i*(2*TEXT_ITEM_BORDER+textPixelSize), 
          fileListBoxWidth - scrollbarWidth , 2*TEXT_ITEM_BORDER+textPixelSize);
        textColor = fileListHLTextColor;
      }
      memset(stringBuffer, 0, 512);
      if ( fileItems[fileItemTop + i].filetype == FS_DIRECTORY) {
        sprintf(stringBuffer, "<%s>", fileItems[fileItemTop + i].longname);
      }
      else {
        sprintf(stringBuffer, "%s", fileItems[fileItemTop + i].longname);
      }
      mainFont->printStringToImage(drawImage, fileListBoxLeft+TEXT_ITEM_BORDER, 
        fileListBoxTop+i*(2*TEXT_ITEM_BORDER+textPixelSize) + textPixelSize-TEXT_ITEM_BORDER,
        fileListBoxWidth - 2*TEXT_ITEM_BORDER - scrollbarWidth,
        textPixelSize,
        textColor,
        stringBuffer); 
    }
  }
  
*/


int searchMusicWithFatDir(const char *path){    
    max=listDir(&path,short);
    for(int n=1;n<=maxItem;n++){
        if(items[n].filetype==FS_DIRECTORY){
            char tmp[1024];
            sprintf(tmp,"%s%c%s",path,'/',items[n].longname);
            searchMusicWithFatDir(tmp);
        }else{
            /*add "strcat(path,strcat('/',items[n].longname))" to play list*/
            sprintf(list[max]->path,"%s%c%s",path,'/',items[n].longname);
            readTags(list[max]->path,1,max);
            ++max;
        }
    }
    return max+1;
}

/*
int loadArtImage(char filename,char type[4],int offset,int length,Image *image){
    if((fileExists(&filename)<0)||length==NULL)return 0;
    else{
        FILE *j=fopen(filename,"r");
        int *buf;
        fread(buf,length,1,j);
        image=image_load_buf(buf,length,IMAGEFORMAT,1);
        fclose(j);
        if(!image)return 0;
        else return 1;
    }
}
*/


int searchMusic(char *beginPath,struct tagInfo *infoList){
    char entry[256];
    int fd,sta,num=0;
    SceIoDirent dir;
  
  strcpy(entry,beginPath);
    fd=sceIoDopen(entry);

    for(;;){
        memset(&dir, 0, sizeof(SceIoDirent));
        sta = sceIoDread(fd, &dir);
        if(sta<0)error(strcat("Error searching files on:",entry),0);
        if(sta==0){sta=sceIoDclose(fd);break;}
 
        if(dir.d_stat.st_attr==FILE){
            ++num;
            if(num>1000)error("Max number of songs is 1000",0);
            music_getTagInfo(strcat(strcat(entry,"/"),dir.d_name));
        }else if(dir.d_stat.st_attr==DIR&&strcmp(dir.d_name,".")&&strcmp(dir.d_name,"..")){
            strcopy(entry,strcat(strcat(entry,"/"),dir.d_name));
            searchMusic(beginPath,infoList);
        }
    }
    return num;
}
